<?php
include "../include/global.php";
include "../include/admin_check.php";
$xtpl = new XTemplate ("reviewWrite.htm");
include "setting.php";
include "../include/function.php";

$old_review_image=$_POST['old_review_image'];
$country_id=intval($_POST['country_id']);
$review_name=formatDataForMySql($_POST['review_name']);
$review_brief=formatDataForMySql($_POST['review_brief']);
$review_content=formatData($_POST['review_content']);
$review_position=intval($_POST['review_position']);
$review_isactive=intval($_POST['review_isactive']);

$ID=intval(_POST('ID'));
$type=$_POST['type'];

//
$date = date("Y-m-d");
$sub_mit = $_POST['sub_mit'];
if($sub_mit=="yes"){
	if($review_name==""){
		$error_review_name ="Bạn phải nhập Tên <br />";
	}
	
	//UPLOAD NEW FILE
	if(!$DELETE_ORIGIN_IMAGE)
	{
		if($err=="")
		{
			if($_FILES['review_image'])
			{
				$review_image=uploadfile($path, $_FILES['review_image'],0,$MAX_FILE_SIZE);
			}
		}
		if($review_image=='-1')
		{
			$error_review_image.="Lỗi, không thể upload";
			$err="yes";
		}
		else if($review_image=='0')
		{
			$error_review_image.="File không đúng kiểu";
			$err="yes";
		}
		else if($review_image=='1')
		{
			$max_file_size=$MAX_FILE_SIZE/1024;
			$error_review_image.="Ảnh quá lớn";
			$err="yes";
		}
		else if($review_image=='2')
		{
			$review_image="";
		}	
		
		$review_image_tmp=$review_image;
	}
	else
	{
		if($_FILES['review_image']['name'])
		{
			$myfile=str_replace(" ","_",$_FILES['review_image']['name']);
			$review_image_tmp=time()."_".$myfile;
			$no_origin=1;
		}
	}
	if($err=="" && $review_image_tmp!="")
	{
		//Create thumbnail
		if($IS_CREATE_THUMB){
			for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
			{
				$MAX_QUALITY[$i]=$MAX_THUMB_WIDTH[$i]>$MAX_THUMB_HEIGHT[$i]?$MAX_THUMB_WIDTH[$i]:$MAX_THUMB_HEIGHT[$i];
				thumbnail_images($_FILES['review_image'], $MAX_THUMB_WIDTH[$i], $MAX_THUMB_HEIGHT[$i], $path, $MAX_QUALITY[$i], "thumb_".$MAX_THUMB_WIDTH[$i], $review_image_tmp, 0, $text); 
			}
		}
		//if($DELETE_ORIGIN_IMAGE)
//		{
//			deletefile($path.$review_image_tmp);
//		}
		if(count($MAX_THUMB_WIDTH) && $no_origin==1)
		{
			$review_image=str_replace(" ","_",$review_image_tmp);
		}
	}
	
	$str_error=$error_review_name;
	if($str_error==""){
		if($type=="ADD"){	
			$sql="INSERT INTO ".pre_table."review(country_id, review_name, review_brief, review_content, review_image, review_position, review_isactive, lang) 
					VALUES	('".$country_id."', '".$review_name."', '".$review_brief."', '".$review_content."', '".$review_image."', '".$review_position."', '".$review_isactive."', '".$lang."')";
			execSQL($sql);
		}else{
						//Create thumbnail
			if($IS_CREATE_THUMB && $old_review_image){
				for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
				{
					$MAX_QUALITY[$i]=$MAX_THUMB_WIDTH[$i]>$MAX_THUMB_HEIGHT[$i]?$MAX_THUMB_WIDTH[$i]:$MAX_THUMB_HEIGHT[$i];
					thumbnail_images('', $MAX_THUMB_WIDTH[$i], $MAX_THUMB_HEIGHT[$i], $path, $MAX_QUALITY[$i], "thumb_".$MAX_THUMB_WIDTH[$i], $old_review_image, 0, $text); 
				}
			}
			
		    if($old_review_image && $review_image)
			{
				deletefile($path.$old_review_image);
				//Delete thumbnail
				for($i=0;$i<count($MAX_THUMB_WIDTH);$i++)
				{
					deletefile($path."thumb_".$MAX_THUMB_WIDTH[$i]."_".$old_review_image);
				}
			}
			if(!$review_image && $old_review_image)
			{
				$review_image=$old_review_image;
			}
			
			$sql="UPDATE ".pre_table."review SET 
						country_id='".$country_id."', 
						review_name='".$review_name."', 
						review_brief='".$review_brief."', 
						review_content='".$review_content."', 
						review_image='".$review_image."', 
						review_position='".$review_position."', 
						review_isactive='".$review_isactive."',
						lang='".$lang."'
						WHERE review_id='".$ID."'";			
			execSQL($sql);
		}			

		$act = $_POST['act'];
		if($act=='save'){
			echo '<script>
				parent.document.frmData.target = "_parent";				
				parent.window.location.href="review.php";
			</script>';
		}else{	
			echo '<script>
				parent.document.frmData.target = "_parent";	
				parent.document.frmData.review_name.focus();			
				parent.window.location.href="reviewWrite.php";
			</script>';
		}	
		
	}else{
		echo '<script>
				parent.document.frmData.target = "_parent";		
				parent.document.getElementById("err").style.display="block";
				parent.document.getElementById("err_string").innerHTML="'.$str_error.'";
			</script>';	
	}
}
?>